<?php
 
ini_set('display_errors', E_ALL);
$GLOBALS['THRIFT_ROOT'] = './php/lib/Thrift';

require_once( $GLOBALS['THRIFT_ROOT'] . '/Exception/TException.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Factory/TStringFuncFactory.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/StringFunc/TStringFunc.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Transport/TTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Exception/TTransportException.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Protocol/TProtocol.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/StringFunc/Core.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Type/TType.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Type/TMessageType.php' );
//require_once( $GLOBALS['THRIFT_ROOT'] . '/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'] . '/Protocol/TBinaryProtocol.php' );
require_once( './gen-php/Hbase/Hbase.php' );
require_once( './gen-php/Hbase/Types.php' );

use Thrift\Transport\TSocket;
use Thrift\Transport\TBufferedTransport;
use Thrift\Protocol\TBinaryProtocol;
use Hbase\HbaseClient;
use Hbase\ColumnDescriptor;
use Hbase\Mutation;

$socket = new TSocket('localhost', '9090');
 
$socket->setSendTimeout(10000); // Ten seconds (too long for production, but this is just a demo ;)
$socket->setRecvTimeout(20000); // Twenty seconds
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new HbaseClient($protocol);
 
$transport->open();
 
//获取表列表
$tables = $client->getTableNames();
sort($tables);
foreach ($tables as $name) {
 
    //echo( "  found: {$name}\n" );
}
 
$tableName = "student";
//创建新表student
if(count($tables) == 0){
		$columns = array(
			new ColumnDescriptor(array(
				'name' => 'id:',
				'maxVersions' => 10
			)),
			new ColumnDescriptor(array(
				'name' => 'name:'
			)),
			new ColumnDescriptor(array(
				'name' => 'score:'
			)),
		);
		 
		try {
			$client->createTable($tableName, $columns);
		} catch (AlreadyExists $ae) {
			echo( "WARN: {$ae->message}\n" );
		}
}
//获取表的描述
 
$descriptors = $client->getColumnDescriptors($tableName);
asort($descriptors);
foreach ($descriptors as $col) {
    //echo( "  column: {$col->name}, maxVer: {$col->maxVersions}\n" );
}
 
//修改表列的数据
$row = '2';
$valid = "123";
$mutations = array(
    new Mutation(array(
        'column' => 'score',
        'value' => $valid
    )),
);
$client->mutateRow($tableName, $row, $mutations);
 
 
//获取表列的数据
$row_name = '2';
$fam_col_name = 'score';
$arr = $client->get($tableName, $row_name, $fam_col_name);
// $arr = array
foreach ($arr as $k => $v) {
// $k = TCell
    echo ("value = {$v->value} ,  \n");
    echo ("timestamp = {$v->timestamp}  \n");
}
 
$arr = $client->getRow($tableName, $row_name);
// $client->getRow return a array
foreach ($arr as $k => $TRowResult) {
// $k = 0 ; non-use
// $TRowResult = TRowResult
    var_dump($TRowResult);
}
 
$transport->close();
?>


